/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class DaftarUser {

    private int jumlahUser = -1;
    private int jumlahAdmin = -1;

    public DaftarUser() {
        emf = Persistence.createEntityManagerFactory("apoltekkPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /**
     * @return the jumlahMember
     */
    public int getJumlahUser() {

        if (jumlahUser == -1) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                Query q = em.createQuery("SELECT count(o) FROM user as o");
                Number jumlah = (Number) q.getSingleResult();
                jumlahUser = jumlah.intValue();

            } catch (javax.persistence.EntityNotFoundException e) {
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        return jumlahUser;
    }

    public int getJumlahAdmin() {

        if (jumlahAdmin == -1) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                Query q = em.createQuery("SELECT count(o) FROM admin as o");
                Number jumlah = (Number) q.getSingleResult();
                jumlahAdmin = jumlah.intValue();

            } catch (javax.persistence.EntityNotFoundException e) {
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        return jumlahAdmin;
    }


    public User getUser(String username){
        User user = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM user as o WHERE o.username = :username");
            q.setParameter("username", username);
            user = (User) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return user;
    }

    public Admin getAdmin(String username){
        Admin ad = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM admin as o WHERE o.username = :username");
            q.setParameter("username", username);
            ad = (Admin) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return ad;
    }

    public void tambahUser(User user) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(user);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }
    public void tambahAdmin(Admin admin) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(admin);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void hapusUser(User user) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(user);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void hapusAdmin(Admin ad) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ad);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void editUser(User user) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(user);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void editAdmin(Admin ad) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ad);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }
}
